package freespiritsocial.io.user.ui.fragment.mine

import android.os.Bundle
import androidx.core.os.bundleOf
import freespiritsocial.io.user.R
import freespiritsocial.io.user.base.BaseFragment
import freespiritsocial.io.user.databinding.FrgResetPasswordBinding
import freespiritsocial.io.user.ext.setTitle
import freespiritsocial.io.user.handle.BackHandle
import freespiritsocial.io.user.viewmodel.ResetPasswordViewModel
import me.hgj.jetpackmvvm.ext.nav
import me.hgj.jetpackmvvm.ext.navigateAction

/**
 * @description 重置密码
 */
class ResetPasswordFrg: BaseFragment<ResetPasswordViewModel,FrgResetPasswordBinding>() {
    override fun layoutId(): Int = R.layout.frg_reset_password

    override fun initView(savedInstanceState: Bundle?) = with(mDatabind) {
        mViewModel.areaCode.set(arguments?.getString("countryCode",""))
        val mobile = arguments?.getString("phoneNumber","")
        mViewModel.mobile.set(mobile)
        mViewModel.isForget.set(mobile.isNullOrEmpty().not())
        viewModel = mViewModel
        click = Click()
        root.setTitle(getString(if (mViewModel.isForget.get()) R.string.reset_password else R.string.change_password))
    }

    inner class Click: BackHandle {

        fun submit() {
            if (mViewModel.isForget.get().not())
                mViewModel.resetPassword {
                    nav().navigateUp()
                }
            else {
                if (mViewModel.confirmPassword.get() != mViewModel.newPassword.get()) {
                    mViewModel.toast.postValue("The passwords entered two times are inconsistent")
                    return
                }
                nav().navigateAction(
                    R.id.action_resetPasswordFrg2_to_forgotVerificationCodeFrg,bundleOf(
                        "phoneNumber" to mViewModel.mobile.get(),
                        "countryCode" to mViewModel.areaCode.get(),
                        "password" to mViewModel.newPassword.get(),
                    )
                )
            }
        }

        override fun goBack() {
            nav().navigateUp()
        }
    }
}